



use  "$clean/clean_all.dta", clear


label var app_before_main  "Downloaded app before survey"
label var days_unres_before_main  "Days canvassed before survey"
label var doors_unres_before_main  "Doors visited before survey"
la var prior_comp "Prior belief: competition"
label var canvass_any_before_main_unres "Has canvassed before survey"
ren canvass_any_before_main_unres  canvass_any_before

reg treatment female age member years_member experience canv_workshop app_before_main canvass_any_before days_unres_before_main doors_unres_before_main_wins prior_comp
  
loc covars "female age member years_member experience canv_workshop app_before_main canvass_any_before days_unres_before_main doors_unres_before_main prior_comp"

/* Balance tables */


    preserve

    clear all
    eststo clear
    estimates drop _all

    set obs 10
    qui gen x = 1
    qui gen y = 1

    forval i = 1/3 {

        qui eststo col`i': reg x y

    }

    restore

/* Statistics */

    loc tabletitle "Randomization check for experiment `expid'"
    loc rowstats ""
    loc rowlabels ""
    loc colnames " "Treatment" "Control" "P-value(Treatment = Control)""

    loc varlength: list sizeof covars
    loc varindex = 1

    mat def P1 = J(`varlength', 1, .)
    mat def P2 = J(`varlength', 1, .)
    mat def P3 = J(`varlength', 1, .)

    foreach var in `covars' {


        cap noi {
            qui sum `var' if treatment == 1  
            estadd loc `var'_m = string(r(mean), "%9.2f"): col1
        }

        cap noi {
            qui sum `var' if treatment == 0 
            estadd loc `var'_m = string(r(mean), "%9.2f"): col2
        }



        cap noi {
            qui reg `var' treatment, vce(r)
            qui test treatment = 0
            estadd loc `var'_m = string(r(p), "%9.3f"): col3
            mat def P3[`varindex', 1] = r(p)
        }
		

*				 cap noi {
 *           qui sum `var' 
 *           estadd loc `var'_m =  string(r(N), "%9.0f"): col4
 *       }

        loc rowstats "`rowstats' `var'_m `var'_sd"
        loc rowlabels "`rowlabels' `"`: var la `var''"' " " "

        loc ++varindex

    }
 
 
 
    esttab * using "$tables/Table_A2.tex", replace cells(none) booktabs nonotes nonum compress alignment(c) nogap noobs nobaselevels label mtitle(`colnames') stats(`rowstats', labels(`rowlabels'))
    eststo clear




